package com.hzlx.products.controller;

import java.util.List;
import java.io.IOException;

import com.hzlx.ProductsDto;
import com.hzlx.ResultDto;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.products.domain.Products;
import com.hzlx.products.service.IProductsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 产品管理Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/base")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;

    /**
     * 查询产品管理列表
     */
    @GetMapping("/products/list")
    public TableDataInfo list(Products products)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @RequiresPermissions("base-data-service:products:export")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/products/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取产品管理详细信息
     */
    @RequiresPermissions("base-data-service:products:query")
    @GetMapping(value = "/products/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productsService.selectProductsByProductId(productId));
    }

    /**
     * 新增产品管理
     */
    @RequiresPermissions("base-data-service:products:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping("/products")
    public AjaxResult add(@RequestBody Products products)
    {
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改产品管理
     */
    @RequiresPermissions("base-data-service:products:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/products")
    public AjaxResult edit(@RequestBody Products products)
    {
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除产品管理
     */
    @RequiresPermissions("base-data-service:products:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/products/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productsService.deleteProductsByProductIds(productIds));
    }


    @RequiresPermissions("base-data-service:products:query")
    @GetMapping(value = "/products/res/{productId}")
    public ResultDto<ProductsDto> getResInfo(@PathVariable("productId") Long productId)
    {
        return ResultDto.<ProductsDto>builder().code(200).data(productsService.selectProductsByProductIdDto(productId)).build();
    }
}
